/*
 * SubTitle.cpp
 *
 *  Created on: 8-ott-2008
 *      Author: fpistonesi
 */
/*

 Copyright (c) 2008-2009 Fabrizio Pistonesi

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

 */

#include "subtitle.h"

SubTitle::SubTitle(IrrlichtDevice* device)
{
  Device = device;
  IsRunning = false;
  IGUIEnvironment* GUI = Device->getGUIEnvironment();
  dimension2d<u32> size = Device->getVideoDriver()->getScreenSize();
  Subtitle = GUI->addStaticText(L"",rect<s32>(0,size.Height-100,size.Width,size.Height),false,true);
  Subtitle->setTextAlignment(EGUIA_CENTER,EGUIA_CENTER);
  Subtitle->setOverrideColor(SColor(255,255,255,255));
  Subtitle->setOverrideFont(GUI->getFont("fonts/verasans12.xml"));
  Subtitle->setBackgroundColor(SColor(127,0,0,0));
  Subtitle->setVisible(false);
  IsRunning = false;

}

SubTitle::~SubTitle()
{
  Subtitle->drop();
}

void SubTitle::print(const wchar_t* string, u32 delay)
{
  //if(!IsRunning)
    //{
      u32 now = Device->getTimer()->getTime();
      EndTime = now + delay;
      IsRunning = true;
      Subtitle->bringToFront(Subtitle);
      Subtitle->setVisible(true);
      Subtitle->setText(string);
    //}
}

void SubTitle::format(const c8* font, SColor color, int size)
{
  IGUIEnvironment* GUI = Device->getGUIEnvironment();
  Subtitle->setOverrideFont(GUI->getFont(font));
  Subtitle->setOverrideColor(color);
}

void SubTitle::draw()
{
  Subtitle->getParent()->bringToFront(Subtitle);
  dimension2d<u32> size = Device->getVideoDriver()->getScreenSize();
  Subtitle->setRelativePosition(rect<s32>(0,size.Height-100,size.Width,size.Height));
  if (IsRunning)
    {
      u32 now = Device->getTimer()->getTime();
      if (now > EndTime)
        {
          Subtitle->setText(L"");
          Subtitle->setVisible(false);
          IsRunning = false;
        }
    }
}

